/* 
 * +--------------------------------------------------------------------------+
 * | Copyright (c) 2009-10 Add This, LLC                                      |
 * +--------------------------------------------------------------------------+
 * | This program is free software; you can redistribute it and/or modify     |
 * | it under the terms of the GNU General Public License as published by     |
 * | the Free Software Foundation; either version 3 of the License, or        |
 * | (at your option) any later version.                                      |
 * |                                                                          |
 * | This program is distributed in the hope that it will be useful,          |
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            |
 * | GNU General Public License for more details.                             |
 * |                                                                          |
 * | You should have received a copy of the GNU General Public License        |
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.    |
 * +--------------------------------------------------------------------------+
 */

AddThis_Util.dbg("main: top");

var AddThis = {
  
	init: function() {
		AddThis_Util.dbg("main.init: entered");
    	
        var newuser = !AddThis_Util.getBooleanPref("extensions.addthis.init", false),
            v2user = !newuser && (AddThis_Util.getStringPref("extensions.addthis.session","")=="");
        
        if (newuser) {
            AddThis_Util.dbg("main.init: first run");
            AddThis_Util.loadServiceDefaults();
    		AddThis_Toolbar.setVisibility(true);
    		AddThis_MainButton.setVisibility(false);
            AddThis_UrlBar.setVisibility(true);
            AddThis_ContextMenu.setVisibility(true);
            AddThis_Util.setBooleanPref("extensions.addthis.init", true);
        } else if (v2user) {
            AddThis_Util.dbg("main.init: upgrade");
            AddThis_Toolbar.setVisibility(true);
        }
        
		
        AddThis_UrlBar.init();
		AddThis_Toolbar.init();
        AddThis_Toolbar.resizeDelay();
		
        
        AddThis_Search.init();
        
		
        var contextMenu = document.getElementById("contentAreaContextMenu");
        contextMenu.addEventListener("popupshowing", AddThis_ContextMenu.setVisibility,true);
        var toolsMenu = document.getElementById("menu_ToolsPopup");
        toolsMenu.addEventListener("popupshowing", AddThis_ToolsMenu.setVisibility,true);
        window.addEventListener("resize", AddThis_Toolbar.resizeDelay, true);
        
        AddThis_Util.png();
        if (newuser || v2user) {
            window.setTimeout(AddThis.showWelcome,1500);
        }
		AddThis_Util.dbg("main.init: exiting");
  	},
    
    showPreferences: function() {
        AddThis_Util.dbg("showPreferences: popping window...");
        var w = window.openDialog('chrome://addthis/content/preferences.xul', 'AddThis Preferences', 'centerscreen,chrome,titlebar,toolbar');
        w.focus();
    },
    
    gotoFeedback: function()
    {
        AddThis_Util.navToUrl(AddThis_Constants.FEEDBACK_URL);
    },
    
    showAbout: function()
    {
        AddThis_Util.dbg("showAbout: popping window...");
        var w = window.openDialog('chrome://addthis/content/about.xul', '', 'centerscreen,chrome,titlebar,toolbar');
        w.focus();
    },
    
    showWelcome: function() {
        AddThis_Util.dbg("showWelcome: opening welcome.xul...");
        AddThis_Util.navToUrl('chrome://addthis/content/welcome.xul');
    },
    
    gotoAddThis: function()
    {
        AddThis_Util.navToUrl("http://www.addthis.com");
    },
    
    shareAddon: function()
    {
        AddThis_Sharing.invokeService('more',AddThis_Constants.AMO_URL,AddThis_Constants.AMO_TITLE);
    },
    
    uninstall: function()
    {
        if (AddThis_Util.fxVersionAtLeast("4.0a"))
        {
            Components.utils.import("resource://gre/modules/AddonManager.jsm");
            AddonManager.getAddonByID(AddThis_Constants.ID,function(addon){
                if (addon)
                {
                    addon.uninstall();
                    BrowserOpenAddonsMgr("extensions");
                }
            });
        }
        else
        {
            var extensionStrings = document.getElementById("atExtensionsStrings");
            var extensionManager = Components.classes["@mozilla.org/extensions/manager;1"]
                                   .getService(Components.interfaces.nsIExtensionManager);
            
            if (!AddThis_Util.fxVersionAtLeast("3.0a"))
                extensionManager = extensionManager.QueryInterface(Components.interfaces.nsIExtensionManager_MOZILLA_1_8_BRANCH);
            
            var dependentItems = extensionManager.getDependentItemListForID(AddThis_Constants.ID, true, { });
    
            
            var name = "AddThis";
            var params = {
                message2: extensionStrings.getFormattedString("uninstallQueryMessage",[name]),
                title: extensionStrings.getFormattedString("uninstallTitle",[name]),
                buttons: {
                  accept: { label: extensionStrings.getString("uninstallButton") },
                  cancel: { label: extensionStrings.getString("cancelButton"), focused: true }
                }
              }
            if (dependentItems.length > 0)
                params.message1 = extensionStrings.getFormattedString("uninstallWarnDependMsg",[name]);
            var names = [];
            for (var i = 0; i < dependentItems.length; ++i)
                names.push(dependentItems[i].name + " " + dependentItems[i].version);
            window.openDialog("chrome://mozapps/content/extensions/list.xul", "","titlebar,modal,centerscreen", names, params);
            
            if (params.result == "accept")
            {
                extensionManager.uninstallItem(AddThis_Constants.ID);
                BrowserOpenAddonsMgr("extensions");
            }
        }
    }
};


window.addEventListener("load", AddThis.init, false);

AddThis_Util.dbg("main: done");

